package bainuo

import com.alibaba.fastjson.JSON

def execute() {
    def conn
    try {
        conn = groovy.sql.Sql.newInstance(mypara.hqcon(2))
        def sql1 = "select top 10 code ,name  from bd_stordoc "
        def sql2 = "select top 10  vtrantypecode from po_arriveorder "
        def selectSql = "select number from WMS_bainuo_log"
//        List<Map<String,String>> items = new ArrayList<>();;
//        Map<String,String> bill = new HashMap<String,String>();
        //def select = conn.firstRow(sql)
        def ckList = conn.rows(sql1 + sql2 )
        def map = conn.firstRow(selectSql)
        // 组装json
        for (def ckitem : ckList) {
            if (map) {
                continue;
            }
            if (ckitem != null) {
                Map<String, Object> root = new HashMap<String, Object>();
                Map<String, Object> bill = new HashMap<String, Object>();
                bill.put("warehouseNo", ckitem.code);
                bill.put("warehouseName", ckitem.name);
                bill.put("billType", ckitem.vtrantypecode);
                root.put("bill", bill);
                List<Map<String, Object>> items = new ArrayList<>();
                List<Map<String, Object>> qitem = new ArrayList<>();
                def sql = "select ntotalastnum from po_arriveorder where vtrantypecode='" + ckitem.vtrantypecode + "'"
                def qitems = conn.rows(sql)
                for (def det : qitems) {
                    Map<String, Object> item = new HashMap<>();
                    item.put("batchNo", det.get("po_arriveorder"));
                    items.add(item);
                }
                root.put("item", items);
                Map<String, String> item = new HashMap<>();
                Map<String, String> bills = new HashMap<>();

//                item.put("id", ckitem.pk_material);
//                item.put("itemNo", ckitem.code);
//                item.put("itemName", ckitem.name);
//                item.put("remarks", ckitem.memo);
//                items.add(item);

            }
        }
        Map<String, List> pushData = new HashMap<>()
        pushData.put("items", items);
        pushData.put("bill", bill);
        push(pushData)
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (conn != null) {
            conn.close()
        }
    }
}

def push(item) {
    HQMHttpUtil.httpFWToMap("http://10.1.1.226:8099/wsdsjmcloud-v12/wbs/funcexe3/WMS_daijieshouwuliao001", item)
}

class HQMHttpUtil {
    public static HashMap<String, Object> account = new HashMap<String, Object>();

    public HQMHttpUtil() {
    }

    public HQMHttpUtil(HashMap<String, Object> account) {
        this.account = account;
    }

    public static Map httpFWToMap(String url, Map mapdata) {
        String json = JSON.toJSONString(mapdata);
        println "请求JSON"
        println json
        //PrintWriter out = null;
//        DataOutputStream out
        OutputStream out
        BufferedReader ins = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            HttpURLConnection conn = realUrl.openConnection();
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");
            conn.setUseCaches(false);
            // 设置通用的请求属性
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("connection", "Keep-Alive");

            conn.connect();

            out = conn.getOutputStream();
            if (!"".equals(json)) {
                //out.writeBytes(json);
                out.write(json.getBytes("UTF-8"));
            }
            out.flush();
            out.close();


            // 定义BufferedReader输入流来读取URL的响应
            //            ins = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            if (conn.getResponseCode() == 200) {
                ins = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            } else {
                ins = new BufferedReader(new InputStreamReader(conn.getErrorStream(), "UTF-8"));
            }

            String line;
            while ((line = ins.readLine()) != null) {
                result += line;
            }
            println("调用WMS返回报文=" + result)
            return JSON.parseObject(result, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("[POST请求]向地址：" + url + " 发送数据：" + json + " 发生错误!" + result + "JSON结束");
        } finally {// 使用finally块来关闭输出流、输入流
            try {
                if (out != null) {
                    out.close();
                }
                if (ins != null) {
                    ins.close();
                }
            } catch (IOException ex) {
                System.out.println("关闭流异常");
            }
        }
        return null;
    }
}
